package com.neuedu.boot.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @author 金山
 * 项目：boot-learn
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/219:00
 */
public class StringUtils {


    private static Map<Pattern,SimpleDateFormat>   patternMap = new HashMap<>();
    static {
        patternMap.put( Pattern.compile("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$"),
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        patternMap.put( Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$"),
                new SimpleDateFormat("yyyy-MM-dd"));
        patternMap.put( Pattern.compile("^\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}$"),
                new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
        patternMap.put( Pattern.compile("^\\d{4}/\\d{2}/\\d{2}$"),
                new SimpleDateFormat("yyyy/MM/dd"));
        patternMap.put( Pattern.compile("^\\d{2}:\\d{2}:\\d{2} \\d{4}-\\d{2}-\\d{2}$"),
                new SimpleDateFormat("HH:mm:ss yyyy-MM-dd"));
    }




    public static Date string2Date(String source){
        //需求,验证字符串，是否满足  四个数字-两个数字-两个数字

        Date date = null;
        Set<Pattern> patterns = patternMap.keySet();
        Iterator<Pattern> it = patterns.iterator();
        while (it.hasNext()){
            Pattern pattern = it.next();
            if(pattern.matcher(source).matches()){
                SimpleDateFormat sdf  = patternMap.get(pattern);

                try {
                    date = sdf.parse(source);
                    break;
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }

       return  date;
    }
}
